package com.gin.no_gay_alliance.adpter.history;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gin.common.utils.TimeUtils;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.bbscode.utils.BbsTagParser;
import com.gin.nga.response.alterlog.ReputationLog;
import com.gin.nga.response.field.AlterInfo;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.activity.TopicReadActivity;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.adpter.reply.OperationLogAdapter;
import com.gin.no_gay_alliance.adpter.reply.ReplyItemAdapter;
import com.gin.no_gay_alliance.customview.ContentWebViewRender;
import com.gin.no_gay_alliance.database.entity.ReplyHistory;
import com.gin.no_gay_alliance.databinding.ItemReplyHistoryBinding;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.gin.no_gay_alliance.utils.ViewUtils;

import java.util.List;
import java.util.Locale;

/**
 * 回复历史适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/18 10:48
 **/
public class ReplyHistoryAdapter extends BaseBindingRecyclerViewAdapter<ItemReplyHistoryBinding, ReplyHistory> implements View.OnClickListener {
    ContentWebViewRender contentWebViewRender;

    public ReplyHistoryAdapter(@NonNull Context context, @NonNull List<ReplyHistory> data) {
        super(context, data);
        contentWebViewRender = new ContentWebViewRender(context, handler);
        contentWebViewRender.setBlockNetworkImage(false);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemReplyHistoryBinding> holder, @NonNull ItemReplyHistoryBinding b, int position, ReplyHistory item) {
        // 用户链接
        b.ulv.init(item.authorUid, item.username, R.drawable.ic_app_g, 20, 0);
        // 匿名标记
        b.tvAnonymous.setVisibility(item.anonymous ? View.VISIBLE : View.GONE);
        // 楼层号
        b.tvFloorNumber.setText(String.format(Locale.CHINA, "%d 楼", item.floorNumber));
        //  标题
        b.tvTitle.setText(item.title);
        // 渲染正文
        contentWebViewRender.render(position, b.layoutContent, holder, BbsTagParser.parseContent(item.content));
        contentWebViewRender.setBlockNetworkImage(false);
        //   记录时间
        b.tvTimestamp.setText(String.format("记录时间: %s", TimeUtils.format(item.timestamp, TimeUtils.SHORT_FORMATTER_2)));
        //   发布时间
        b.tvPostTime.setText(TimeUtils.format(item.postDatetime, TimeUtils.SHORT_FORMATTER_2));

        // 操作日志相关views
        View[] logViews = {b.tvOperationLog, b.dividerOperationLog, b.rvOperationLog};
        if (!ObjectUtils.isEmpty(item.alterinfoRaw)) {
            final String s = item.alterinfoRaw.replace("||", "");
            AlterInfo alterInfo = new AlterInfo(s);
            // 最后修改时间
            if (alterInfo.getLastEdit() != null) {
                b.tvLastEdit.setText(ReplyItemAdapter.toStringLastEdit(alterInfo.getLastEdit()));
            }
            //  操作日志
            List<ReputationLog> logs = alterInfo.getLogs();
            // 控制是否显示
            ViewUtils.setGone(ObjectUtils.isEmpty(logs), logViews);
            if (!ObjectUtils.isEmpty(logs)) {
                b.rvOperationLog.setAdapter(new OperationLogAdapter(context, logs));
            }
        } else {
            b.tvLastEdit.setText("");
            ViewUtils.setGone(true, logViews);
        }
        //   历史版本按钮
        boolean isEmpty = ObjectUtils.isEmpty(item.versions);
        ViewUtils.setGone(isEmpty, b.tvVersions);
        b.tvVersions.setTag(item.versions);
        if (!isEmpty) {
            b.tvVersions.setText(String.format(Locale.CHINA, "历史版本(%d)", item.versions.size()));
        }

        b.tvOriginalReply.setTag(item);

        ViewUtils.setOnClickListener(this, b.tvVersions, b.tvOriginalReply);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        // 历史版本
        if (v.getId() == R.id.tv_versions) {
            List<ReplyHistory> data = (List<ReplyHistory>) v.getTag();
            showReplyHistoryDialog(context, data);
        }
        // 打开原帖
        if (v.getId() == R.id.tv_original_reply) {
            ReplyHistory item = (ReplyHistory) v.getTag();
            if (item != null) {
                TopicReadActivity.startActivity(context, null, item.topicId, item.page, item.replyId, null);
            }
        }

    }

    /**
     * 弹窗展示一个历史版本列表
     *
     * @param context
     * @param data
     */
    public static void showReplyHistoryDialog(@NonNull Context context, List<ReplyHistory> data) {
        RecyclerView rv = new RecyclerView(context);
        rv.setLayoutManager(new LinearLayoutManager(context));
        rv.setAdapter(new ReplyHistoryAdapter(context, data));
        AlertDialogUtils.showMessage(context, "历史版本", null, rv);
    }
}
